package cn.iocoder.yudao.module.erp.controller.admin.giitem.giItemgz;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.giitem.giItemgz.vo.GiItemGzPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.giitem.giItemgz.vo.GiItemGzRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.giitem.giItemgz.vo.GiItemGzSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.giitem.GiItemGzDO;
import cn.iocoder.yudao.module.erp.service.giitem.GiItemGzService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP物流广州仓库货号详情")
@RestController
@RequestMapping("/erp/gi-item-gz")
@Validated
public class GiItemGzController {

    @Resource
    private GiItemGzService giItemGzService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP物流广州仓库货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:gi-item-gz:create')")
    public CommonResult<String> createGiItemGz(@Valid @RequestBody GiItemGzSaveReqVO createReqVO) {
        return success(giItemGzService.createGiItemGz(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP物流广州仓库货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:gi-item-gz:update')")
    public CommonResult<Boolean> updateGiItemGz(@Valid @RequestBody GiItemGzSaveReqVO updateReqVO) {
        giItemGzService.updateGiItemGz(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP物流广州仓库货号详情")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:gi-item-gz:delete')")
    public CommonResult<Boolean> deleteGiItemGz(@RequestParam("id") Long id) {
        giItemGzService.deleteGiItemGz(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP物流广州仓库货号详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:gi-item-gz:query')")
    public CommonResult<GiItemGzRespVO> getGiItemGz(@RequestParam("id") Long id) {
        GiItemGzDO giItemGz = giItemGzService.getGiItemGz(id);
        return success(BeanUtils.toBean(giItemGz, GiItemGzRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP物流广州仓库货号详情分页")
    // @PreAuthorize("@ss.hasPermission('erp:gi-item-gz:query')")
    public CommonResult<PageResult<GiItemGzRespVO>> getGiItemGzPage(@Valid GiItemGzPageReqVO pageReqVO) {
        pageReqVO.setPageSize(-1);
        PageResult<GiItemGzDO> pageResult = giItemGzService.getGiItemGzPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GiItemGzRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP物流广州仓库货号详情 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:gi-item-gz:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGiItemGzExcel(@Valid GiItemGzPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GiItemGzDO> list = giItemGzService.getGiItemGzPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP物流广州仓库货号详情.xls", "数据", GiItemGzRespVO.class,
                BeanUtils.toBean(list, GiItemGzRespVO.class));
    }

}